<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="login-logo">R</div>
        <h1 class="login-title">通用商品后台管理系统</h1>
        <p class="login-subtitle">古之立大志者，不惟有超世之才，亦必有坚韧不拔之志</p>
      </div>
      
      <div class="login-form-container">
        <div class="form-panel">
          <div class="form-title">用户登陆</div>
          
          <el-form 
            ref="loginFormRef"
            :model="form" 
            :rules="rules" 
            @submit.prevent="handleLogin"
            class="login-form"
          >
            <el-form-item prop="username">
              <label class="form-label">用户名</label>
              <el-input
                v-model="form.username"
                placeholder="admin"
                size="large"
                clearable
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <label class="form-label">密码</label>
              <el-input
                v-model="form.password"
                type="password"
                placeholder="******"
                size="large"
                show-password
                clearable
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                size="large"
                :loading="loading"
                @click="handleLogin"
                class="login-button"
              >
                {{ loading ? '登录中...' : '登陆' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <el-alert
            v-if="message"
            :title="message"
            :type="messageType"
            :closable="false"
            show-icon
            style="margin-top: 20px"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { User, Lock, Right } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'Login',
  components: {
    User,
    Lock,
    Right
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    const loginFormRef = ref()
    
    const form = reactive({
      username: '',
      password: ''
    })
    
    const rules = reactive({
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度应为3-20位', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ]
    })
    
    const loading = ref(false)
    const message = ref('')
    const messageType = ref('')
    
    const showMessage = (msg, type = 'info') => {
      message.value = msg
      messageType.value = type
      setTimeout(() => {
        message.value = ''
      }, 5000)
    }
    
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      try {
        await loginFormRef.value.validate()
      } catch (error) {
        return
      }
      
      loading.value = true
      message.value = ''
      
      try {
        const result = await authStore.login({
          username: form.username,
          password: form.password
        })
        
        if (result.success) {
          ElMessage.success('登录成功！')
          router.push('/')
        } else {
          showMessage(result.message, 'error')
        }
      } catch (error) {
        showMessage('登录失败: ' + error.message, 'error')
      } finally {
        loading.value = false
      }
    }
    
    return {
      loginFormRef,
      form,
      rules,
      loading,
      message,
      messageType,
      handleLogin,
      User,
      Lock,
      Right
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="puzzle" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse"><path d="M0,0 L10,0 L10,10 L20,10 L20,20 L10,20 L10,30 L0,30 Z" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23puzzle)"/></svg>');
  opacity: 0.3;
}

.login-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.login-logo {
  width: 60px;
  height: 60px;
  background: white;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  color: #409EFF;
  font-size: 24px;
  font-weight: bold;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.login-subtitle {
  font-size: 14px;
  opacity: 0.9;
  max-width: 400px;
  line-height: 1.5;
}

.login-form-container {
  width: 100%;
  max-width: 500px;
  background: #f0f8ff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.1);
  overflow: hidden;
  position: relative;
}

.login-form-container::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 8px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.login-form-container::after {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 8px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.form-panel {
  padding: 40px;
  background: #f0f8ff;
}

.form-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
}

.login-form {
  width: 100%;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(79, 172, 254, 0.3);
}

/* Element UI 样式覆盖 */
:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  border-color: #4facfe;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #4facfe;
  box-shadow: 0 0 0 2px rgba(79, 172, 254, 0.2);
}

:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
  font-size: 14px;
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    max-width: 95%;
  }
  
  .login-title {
    font-size: 20px;
  }
  
  .form-panel {
    padding: 30px 20px;
  }
}

@media (max-width: 480px) {
  .login-title {
    font-size: 18px;
  }
  
  .form-panel {
    padding: 20px 15px;
  }
  
  .login-button {
    height: 44px;
    font-size: 14px;
  }
}
</style>